package com.yf.ability.live.utils;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yf.ability.live.config.CfgLiveAliyunDTO;
import com.yf.ability.live.dto.LiveBuildDTO;
import com.yf.base.utils.file.MD5Util;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 阿里云直播构造
 * @author bool
 */
public class AliAddrBuilder {

    /**
     * M3U8播放地址
     */
    private static final String PUSH_STR = "/exam/{streamName}-{time}-0-0-{pushKey}";
    private static final String PUSH_URL = "rtmp://{pushDomain}/exam/{streamName}?auth_key={time}-0-0-{auth}";



    /**
     * M3U8播放地址
     */
    private static final String PULL_STR = "/exam/{streamName}-{time}-0-0-{pullKey}";
    private static final String PULL_URL = "rtmp://{pullDomain}/exam/{streamName}?auth_key={time}-0-0-{auth}";

    /**
     * M3U8播放地址
     */
    private static final String M3U8_STR = "/exam/{streamName}.m3u8-{time}-0-0-{pullKey}";
    private static final String M3U8_URL = "https://{pullDomain}/exam/{streamName}.m3u8?auth_key={time}-0-0-{auth}";

    /**
     * M3U8播放地址
     */
    private static final String FLV_STR = "/exam/{streamName}.flv-{time}-0-0-{pullKey}";
    private static final String FLV_URL = "https://{pullDomain}/exam/{streamName}.flv?auth_key={time}-0-0-{auth}";

    /**
     * UDP播放地址
     */
    private static final String UDP_STR = "/exam/{streamName}-{time}-0-0-{pullKey}";
    private static final String UDP_URL = "artc://{pullDomain}/exam/{streamName}?auth_key={time}-0-0-{auth}";


    /**
     * 生成推流和播流信息
     */
    public static LiveBuildDTO generate(CfgLiveAliyunDTO conf, String streamName) {

        // 如果为空，则创建
        if(streamName == null){
            streamName = IdWorker.getIdStr();
        }

        LiveBuildDTO dto = new LiveBuildDTO();

        // 时间戳
        String time = dateToStamp();

        dto.setPushUrl(buildUrl(conf, streamName, time, PUSH_STR, PUSH_URL));
        dto.setPullUrl(buildUrl(conf, streamName, time, PULL_STR, PULL_URL));
        dto.setM3u8Url(buildUrl(conf, streamName, time, M3U8_STR, M3U8_URL));
        dto.setFlvUrl(buildUrl(conf, streamName, time, FLV_STR, FLV_URL));
        dto.setUdpUrl(buildUrl(conf, streamName, time, UDP_STR, UDP_URL));

        return dto;
    }


    /**
     * 构建URL
     * @param conf
     * @param streamName
     * @param time
     * @param str
     * @param url
     * @return
     */
    public static String buildUrl(CfgLiveAliyunDTO conf, String streamName, String time, String str, String url){

        // 基础变量
        Map<String,String> map = new HashMap<>(16);
        map.put("{streamName}", streamName);
        map.put("{time}", time);
        map.put("{pushDomain}", conf.getPushDomain());
        map.put("{pushKey}", conf.getPushKey());
        map.put("{pullDomain}", conf.getPullDomain());
        map.put("{pullKey}", conf.getPullKey());

        for(String key: map.keySet()){
            str = str.replace(key, map.get(key));
            url = url.replace(key, map.get(key));
        }

        // 生成秘钥
        String auth = MD5Util.MD5(str);
        url = url.replace("{auth}", auth);
        return url;
    }

    /**
     * 构建播放时长
     * @return
     */
    private static String dateToStamp() {

        Calendar cl = Calendar.getInstance();
        cl.setTimeInMillis(System.currentTimeMillis());
        cl.add(Calendar.MINUTE, 300);

        return String.valueOf(cl.getTimeInMillis() / 1000);
    }


    public static void main(String[] args) {

        CfgLiveAliyunDTO conf = new CfgLiveAliyunDTO();

        //推流域名和鉴权
        String pushHost = "pili.yfhl.net";
        String pushKey = "2pKVftEDY4";

        // 播流域名和鉴权
        String pullHost = "live.yfhl.net";;
        String pullKey = "uy5eyQWGNh";

        conf.setPushDomain(pushHost);
        conf.setPushKey(pushKey);
        conf.setPullDomain(pullHost);
        conf.setPullKey(pullKey);
        LiveBuildDTO dto = generate(conf, "1363506122429952001");
        System.out.println(JSON.toJSONString(dto));
    }


}
